//
//  HomeRecommendShopView.swift
//  VastSwift
//
//  Created by BestVast on 2021/5/30.
//

import UIKit

/// 推荐商家view
class HomeRecommendShopView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    var shops: [String: String]! {
        didSet {
            if shops.count > 0 {
                let cellheight = 60
                var recoLineNum = shops.count / 2
                if shops.count % 2 > 0 {
                    recoLineNum += 1
                }
                
                var recoHeight: CGFloat = CGFloat(CGFloat(recoLineNum) * CGFloat(cellheight));
                if recoLineNum > 1  {
                    recoHeight += CGFloat((recoLineNum - 1) * 10)
                }
                collectView.height = recoHeight
                collectView.reloadData()
            }


        }
    }
    func uiConfig() {
        self.backgroundColor = .white
        self.addSubview(self.collectView)
        
        let titleView: HSectionTitleView = HSectionTitleView.init(frame: CGRect.init(x: 0, y: 10, width: screenWidth - 40, height: 70))
        titleView.titleLabel.text = "推荐商家"
        titleView.subTitleLabel.text = "发现推荐优质商家"
        self.addSubview(titleView)
    }
    lazy var collectView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        let width = (screenWidth - 50) / 2
        layout.itemSize = CGSize(width: width , height: 60)
        layout.minimumLineSpacing=10
        layout.minimumInteritemSpacing=10

        let frame = CGRect.init(x: 10, y: 80, width: screenWidth - 40, height: 220)
        let collectView = UICollectionView.init(frame: frame, collectionViewLayout: layout)
        collectView.backgroundColor = .white
        collectView.delegate = self
        collectView.dataSource = self
        collectView.showsVerticalScrollIndicator = false
        collectView.register(HRecoShopCell.self, forCellWithReuseIdentifier: "HRecoShopCell")
        return collectView
    }()
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if nil != shops && shops.count > 0 {
            return shops.count
        }
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellString = "HRecoShopCell"
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath) as? HRecoShopCell
        cell?.backgroundColor = colorTheme
        cell?.titleLabel.text = "供应商内部"
        return cell!
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        uiConfig()

        
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
